package com.adingxiong.pm.controller;


import com.adingxiong.pm.entity.Devcodetype;
import com.adingxiong.pm.entity.Devcodevalue;
import com.adingxiong.pm.model.Result;
import com.adingxiong.pm.model.RetCode;
import com.adingxiong.pm.server.IDevcodetypeService;
import com.adingxiong.pm.server.IDevcodevalueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.naming.Name;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xiongc
 * @since 2020-03-05
 */
@RestController
@RequestMapping("/codetype")
@Api(tags = "代码表相关")
public class DevcodetypeController {

    @Autowired
    private IDevcodetypeService service;

    @Autowired
    private IDevcodevalueService devcodevalueService;

    @ApiOperation(value = "查询所有的代码表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "条数", dataType = "int", paramType = "query")
    })
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public Result getList(Integer page, Integer size) {
        Page<Devcodetype> list = service.page(new Page<>(page, size));
        return new Result<>().setCode(RetCode.SUCCESS).setMsg("查询成功").setData(list);
    }

    @ApiOperation(value = "添加代码表")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result AddInfo(@RequestBody Devcodetype devcodetype) {
        if (devcodetype == null) {
            return new Result<>().setCode(RetCode.FAIL).setMsg("参数为空，请重试");
        }
        boolean insert = service.save(devcodetype);
        if (!insert) {
            return new Result<>().setCode(RetCode.FAIL).setMsg("系统异常请于管理员联系，请重试").setData(false);
        }
        return new Result<>().setCode(RetCode.SUCCESS).setMsg("数据添加成功").setData(true);
    }

    @ApiOperation(value = "修改代码表信息")
    @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Result update(@RequestBody Devcodetype devcodetype) {
        if (devcodetype == null) {
            return new Result<>().setCode(RetCode.FAIL).setMsg("参数为空，请重试");
        }
        boolean update = service.updateById(devcodetype);
        if (!update) {
            return new Result<>().setCode(RetCode.FAIL).setMsg("系统异常请于管理员联系，请重试").setData(false);
        }
        return new Result<>().setCode(RetCode.SUCCESS).setMsg("数据修改成功").setData(true);
    }

    @ApiOperation(value = "删除代码表")
    @Transactional
    @ApiImplicitParams({
            @ApiImplicitParam(name ="id" ,value = "主键id" ,dataType = "int" ,paramType = "query",required = true)
    })
    @RequestMapping(value = "/del" ,method =RequestMethod.DELETE)
    public Result delete(Integer id) {
        Devcodetype devcodetype = service.getById(id);
        if (devcodetype == null) {
            return new Result<>().setCode(RetCode.FAIL).setMsg("不存在该数据，删除失败");
        }
        //这里删除代码表的同时 ，需要删除对应的代码值
        String name = devcodetype.getCodename();
        QueryWrapper<Devcodevalue> queryWrapper = new QueryWrapper<>();
        Map<String, Object> param = new HashMap<>();
        param.put("codetypename", name);
        param.put("codetyid", id);
        queryWrapper.allEq(param);
        List<Devcodevalue> list = devcodevalueService.list(queryWrapper);
        List<Integer> ids = new ArrayList<>();
        if (list != null) {
            for (Devcodevalue bean : list) {
                ids.add(bean.getId());
            }
        }
        boolean del = service.removeById(id);
        if (del) {
            boolean delAll =devcodevalueService.removeByIds(ids);
            return new Result<>().setCode(RetCode.SUCCESS).setMsg("数据删除成功").setData(true);
        }
        return new Result<>().setCode(RetCode.FAIL).setMsg("数据删除失败").setData(false);
    }


    @ApiOperation(value = "查询相信信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "主键id" ,dataType = "int" ,required = true, paramType = "query")
    })
    @RequestMapping(value = "/getone" ,method = RequestMethod.GET)
    public Result getInfo (Integer id ){
        Devcodetype info = service.getById(id);
        return new Result<>().setCode(RetCode.SUCCESS).setMsg("查询成功").setData(info);
    }


}

